<?php
namespace App\Http\Controllers\User;

use App\Http\Common\BaseController;
use App\Models\Banner;
use App\Models\BannerType;
use App\Models\CommunityIntroduce;
use App\Models\CommunityAward;
use App\Models\Introduce;
use App\Models\User;
use Illuminate\Http\Request;

class IndexController extends BaseController
{
    /**
     * 首页头部轮播图
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function indexTopBanner(){
        $bannerTypeId = BannerType::where('name','=','首页轮播图')->select('id')->get()->toArray();

        foreach ($bannerTypeId as $k => $v){
            $bannerUrl =  Banner::where('banner_type_id','=',$v['id'])->get(['id','url']);
        }
        return $this->returnJson(200,'获取成功',$bannerUrl);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function addCommunityIntroduces(Request $request){
        $introduce = $request->get('introduce');
        $qrcode = $request->get('qrcode');
        $data = [
            'community_introduce'   => $introduce,
            'president_qrcode'      => $qrcode,
            'update_time'           => time()
        ];
        if (!CommunityIntroduce::insert($data)){
            return $this->returnJson(4004,'新增失败');
        }
        return $this->returnJson(200,'新增成功');
    }

    /**
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function getCommunityIntroduces(){
        $data = CommunityIntroduce::where('id','=',1)->get(['id','community_introduce','grade_id','president_qrcode','introduce_image']);
        if (!count($data)){
            return $this->returnJson(200,'没有社团介绍');
        }
        return $this->returnJson(200,'获取成功',$data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function addCommunityAward(Request $request){
        //年级id
        $gid = $request->get('grade_id');
        $award_img = $request->get('award_img');
        $award_content = $request->get('award_content');
        $data = [
            'grade_id'      => $gid,
            'award_img'     => $award_img,
            'award_content' => $award_content,
            'create_time'   => time()
        ];
        if (!CommunityAward::insert($data)){
            return $this->returnJson(4004,'新增失败');
        }
        return $this->returnJson(200,'新增成功');
    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
    */
    public function getCommunityAward(){
        $data = CommunityAward::get(['id','award_username','award_img','award_content','create_time']);
        if (!count($data)){
            return $this->returnJson(200,'社团没有获奖');
        }
        return $this->returnJson(200,'获取成功',$data);
    }

    /**
     * 首页获取人员介绍
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function personIntroduction(){
        $info = Introduce::get(['introduce','image','user_id'])->toArray();
        $data = [];
        foreach($info as $k => $v){
            $username = User::where('id','=',$v['user_id'])->first('username');
            if (!empty($username['username'])) {
                $v['username'] = $username['username'];
                array_push($data,$v);
            }
        }
        return $this->returnJson(200,'获取信息成功',$data);
    }

    /**
     * 首页获取学习环境
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function learnEnvironment(){
        $bannerTypeId = BannerType::where('name','=','学习环境')->select('id')->get()->toArray();
        foreach ($bannerTypeId as $k => $v){
            $bannerUrl =  Banner::where('banner_type_id','=',$v['id'])->get(['id','url']);
        }
        return $this->returnJson(200,'获取成功',$bannerUrl);
    }
}
